<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class calon_siswa extends CI_Controller {
	
	var $objectid = 'calon_siswa';
	
	function __construct(){
		parent::__construct();
		$this->load->model('sd/psb/mdl_calon_siswa');
		$this->load->model('sd/setting/mdl_proses_penerimaan');
		$this->load->model('sd/setting/mdl_kelompok_penerimaan');
		//$this->output->enable_profiler(TRUE);
	}
	
	function index(){
		$data['objectid'] = $this->objectid;
		$this->load->view('sd/psb/calon_siswa', $data);
	}
	
	function grid(){
		$data = $this->mdl_calon_siswa->get_data();
		echo $this->mdl_calon_siswa->togrid($data['row_data'], $data['row_count']);
	}
	
	function add(){
		$data['objectid'] = $this->objectid;
		
		$data['aksi'] = 'add';
		
		$data['id'] = '';
        $data['no_pendaftaran'] = '';
        $data['id_psb'] = '';
        $data['id_kelompok'] = '';
        $data['tahun_masuk'] = $this->mdl_calon_siswa->get_tahunmasuk();
        $data['uang_sumbangan'] = '';
        $data['nama'] = '';
        $data['jk'] = 'L';
        $data['tmp_lahir'] = '';
        $data['tgl_lahir'] = '';
        $data['Agama'] = '';
        $data['kewarganegaraan'] = 'WNI';
        $data['anakke'] = '';
        $data['jml_saudara'] = '';
        $data['bahasa'] = '';
        $data['foto'] = '';
        $data['alamat'] = '';
        $data['tlp'] = '';
        $data['asal_sekolah'] = '';
        $data['gol_darah'] = '';
        $data['riwayat_penyakit'] = '';
        $data['nama_ayah'] = '';
        $data['nama_ibu'] = '';
        $data['status_ayah'] = '';
        $data['status_ibu'] = '';
        $data['alm_ayah'] = '';
        $data['alm_ibu'] = '';
        $data['tmp_lahir_ayah'] = '';
        $data['tmp_lahir_ibu'] = '';
        $data['tgl_lahir_ayah'] = '';
        $data['tgl_lahir_ibu'] = '';
        $data['pendidikan_ayah'] = '';
        $data['pendidikan_ibu'] = '';
        $data['pekerjaan_ayah'] = '';
        $data['pekerjaan_ibu'] = '';
        $data['penghasilan_ayah'] = '';
        $data['penghasilan_ibu'] = '';
        $data['nama_wali'] = '';
        $data['alamat_ortu'] = '';
        $data['tlp_ortu'] = '';
        $data['status'] = '';
		
		$this->load->view('sd/psb/calon_siswa_form', $data);
	}
	
	function edit($id){
		$data['objectid'] = $this->objectid;
		
		$r = $this->mdl_calon_siswa->get_data_edit($id);

		$data['aksi'] = 'edit';
		
		$data['id'] = $r->row()->id;
        $data['no_pendaftaran'] = $r->row()->no_pendaftaran;
        $data['id_psb'] = $r->row()->id_psb;
        $data['id_kelompok'] = $r->row()->id_kelompok;
        $data['tahun_masuk'] = $r->row()->tahun_masuk;
        $data['uang_sumbangan'] = $r->row()->uang_sumbangan;
        $data['nama'] = $r->row()->nama;
        $data['jk'] = $r->row()->jk;
        $data['tmp_lahir'] = $r->row()->tmp_lahir;
        $data['tgl_lahir'] = FormatDateFromMysql($r->row()->tgl_lahir);
        $data['Agama'] = $r->row()->Agama;
        $data['kewarganegaraan'] = $r->row()->kewarganegaraan;
        $data['anakke'] = $r->row()->anakke;
        $data['jml_saudara'] = $r->row()->jml_saudara;
        $data['bahasa'] = $r->row()->bahasa;
        $data['foto'] = $r->row()->foto;
        $data['alamat'] = $r->row()->alamat;
        $data['tlp'] = $r->row()->tlp;
        $data['asal_sekolah'] = $r->row()->asal_sekolah;
        $data['gol_darah'] = $r->row()->gol_darah;
        $data['riwayat_penyakit'] = $r->row()->riwayat_penyakit;
        $data['nama_ayah'] = $r->row()->nama_ayah;
        $data['nama_ibu'] = $r->row()->nama_ibu;
        $data['status_ayah'] = $r->row()->status_ayah;
        $data['status_ibu'] = $r->row()->status_ibu;
        $data['alm_ayah'] = $r->row()->alm_ayah;
        $data['alm_ibu'] = $r->row()->alm_ibu;
        $data['tmp_lahir_ayah'] = $r->row()->tmp_lahir_ayah;
        $data['tmp_lahir_ibu'] = $r->row()->tmp_lahir_ibu;
        $data['tgl_lahir_ayah'] = FormatDateFromMysql($r->row()->tgl_lahir_ayah);
        $data['tgl_lahir_ibu'] = FormatDateFromMysql($r->row()->tgl_lahir_ibu);
        $data['pendidikan_ayah'] = $r->row()->pendidikan_ayah;
        $data['pendidikan_ibu'] = $r->row()->pendidikan_ibu;
        $data['pekerjaan_ayah'] = $r->row()->pekerjaan_ayah;
        $data['pekerjaan_ibu'] = $r->row()->pekerjaan_ibu;
        $data['penghasilan_ayah'] = $r->row()->penghasilan_ayah;
        $data['penghasilan_ibu'] = $r->row()->penghasilan_ibu;
        $data['nama_wali'] = $r->row()->nama_wali;
        $data['alamat_ortu'] = $r->row()->alamat_ortu;
        $data['tlp_ortu'] = $r->row()->tlp_ortu;
		
		$this->load->view('sd/psb/calon_siswa_form', $data);
	}
	
	function save($aksi){
		# init
		$status = "";
		$result = false;
		$data['pesan_error'] = '';
		$id = 0; // id calon siswa
		
		# get post data
		foreach($_POST as $key => $value){
			$data[$key] = $value;
		}
		//$data['nama_kelompok'] = strtoupper($data['nama_kelompok']);
		
		# rules validasi form
		$this->form_validation->set_rules('no_pendaftaran', 'No Pendaftaran', 'trim|required|xss_clean');
        
		# message rules
		$this->form_validation->set_message('required', 'Field %s harus diisi.');
		$this->form_validation->set_message('numeric', 'Field %s harus diisi dengan angka.');
		$this->form_validation->set_message('email', 'Field %s harus diisi dengan alamat email yang benar.');

		$data['pesan_error'] = '';
		if ($this->form_validation->run() == FALSE){
			$data["pesan_error"].= trim(form_error('no_pendaftaran',' ',' '))==''?'':form_error('no_pendaftaran',' ',' ').'<br>';
			
		}else{
			if($aksi=="add"){ // add
				$result = !$this->mdl_calon_siswa->no_pendaftaran_exist($data['no_pendaftaran'], $data['id_psb']);
				if($result){
					$result = $this->mdl_calon_siswa->insert($data);
				}else{
					$data["pesan_error"].='No. pendaftaran '.$data['no_pendaftaran'].' sudah terdapat dalam database! ganti dengan yang lain';
				}
			}else{ // edit
				$result=$this->mdl_calon_siswa->update($data);
			}
		}
		
		if($result){
			$id = $this->mdl_calon_siswa->get_ai()-1;
			echo json_encode(array('success'=>true, 'id'=>$id));
		}else{
			echo json_encode(array('msg'=>$data['pesan_error']));
		}
	}
	
	function delete($kode){
		$result = $this->mdl_calon_siswa->delete($kode);
		if ($result){
			echo json_encode(array('success'=>true));
		} else {
			echo json_encode(array('msg'=>'Data gagal dihapus'));
		}
	}

	function print_bukti($id){ // print bukti pendaftaran
		$this->load->library('HTML2PDF');
		$html2pdf = new HTML2PDF('P', 'A4', 'fr');
		$html2pdf->setDefaultFont('Arial');
		
		//$data['nama'] = 'indriyanto';
		$data['nama_sekolah'] = 'SD NEGERI SUMBER SARI INDAH';
		$data['alamat_sekolah'] = 'Jln. Sumber Makmur No.32, Telp.(0967)581562';
		
		// ambil data dari tabel
		$data['res'] = $this->mdl_calon_siswa->get_data_edit($id);
		
		$konten = $this->load->view('sd/psb/calon_siswa_rpt', $data, true);
		
		$html2pdf->writeHTML($konten, false);
		
		$html2pdf->Output('bukti_pendaftaran.pdf');
	}
	
}